package com.ce.pms.report.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ce.pms.charge.dao.IChargeItemDao;
import com.ce.pms.charge.dao.IParameterDefDao;
import com.ce.pms.charge.model.ChargeItemDomain;
import com.ce.pms.charge.model.ParameterDefDomain;
import com.ce.pms.report.dao.IReportDao;
import com.ce.pms.report.queryvo.ManagerPayTenementCollectQueryVO;
import com.ce.pms.report.vo.ManagerPayTenementCollectReportVO;
import com.ce.report.entity.CEReportDataSet;
import com.ce.report.exception.CEReportException;
import com.ce.report.service.ICEReportDataSetService;
import com.ce.report.vo.ReportsQueryBaseVo;
import com.ce.util.BlankUtil;
import com.ce.util.DateUtil;

/**
 *
 * 功能描述：单项费用分户汇总表（居民管理费分户汇总） Service
 * <p>版权所有：中企动力
 * <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author 黄彦军
 * Created on 2008-11-3
 * Edit author:
 * Edit on:
 * 修改原因:
 */
public class ManagerPayTenementCollectService implements ICEReportDataSetService {

	private IReportDao reportDao;

	private IParameterDefDao parameterDefDao;

	private IChargeItemDao chargeItemDao;

	public void setParameterDefDao(IParameterDefDao parameterDefDao) {
		this.parameterDefDao = parameterDefDao;
	}

	public CEReportDataSet getReportDataSet(ReportsQueryBaseVo arg) throws CEReportException {
		ManagerPayTenementCollectQueryVO queryVO = (ManagerPayTenementCollectQueryVO) arg;

		return new CEReportDataSet(getData(queryVO),getHeadInfo(queryVO));
	}

	/**
	 * 功能描述:获取列表数据  <br>
	 * @param queryVO
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-11-13  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private List<ManagerPayTenementCollectReportVO> getData(ManagerPayTenementCollectQueryVO queryVO){
		String precinctId = queryVO.getPrecinct();
		String buildingId ="";
		if(BlankUtil.isBlank(queryVO.getBuildingId())){
			buildingId = "%";
		}else{
			buildingId = queryVO.getBuildingId().toString();
		}
		String period = DateUtil.convertDateToStr(queryVO.getComputeMonth(), "yyyyMM");
		Long chargeItemId = getChargeItemId(queryVO.getChargeItemKey()); //收费项目Id
		Object [] obj = new Object[]{period,period,period,precinctId,buildingId,chargeItemId};

		List<Object[]> temp = reportDao.getDataList("managerPayTenementCollect", obj);
		List<ManagerPayTenementCollectReportVO> list = arrayToVo(temp);

		return list;
	}

	/**
	 * 功能描述:根据pkey 查找 收费项目id  <br>
	 * @param pkey
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-3  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private Long getChargeItemId (String pkey){
		ParameterDefDomain defDomain = parameterDefDao.findUniqueBy("pkey", pkey);
		Long pvalue = defDomain.getPvalue();//交易码Id
		if (BlankUtil.isBlank(pvalue))
			return null;

		ChargeItemDomain itemDomain =  chargeItemDao.findChargeItemDoByBizCode(pvalue);
		if (BlankUtil.isBlank(itemDomain)){
			return null;
		}

		return itemDomain.getKeyword();
	}


	/**
	 * 功能描述:对象数组 转为 vo数组  <br>
	 * @param temp
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-11-14  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private List<ManagerPayTenementCollectReportVO> arrayToVo(List<Object[]> temp){
		if(BlankUtil.isBlank(temp)){
			return null;
		}

		List<ManagerPayTenementCollectReportVO> list = new ArrayList<ManagerPayTenementCollectReportVO>();
		for (Object[] obj : temp) {
			ManagerPayTenementCollectReportVO reportVO = new ManagerPayTenementCollectReportVO();
			reportVO.setHouseNumber((String)obj[0]);	//房号
			reportVO.setArea((BigDecimal)obj[1]);		//大楼名称
			reportVO.setUserName((String)obj[2]);		//客户
			reportVO.setActualBefore(setValue(obj[3]));//实收往月
			reportVO.setActualThis(setValue(obj[4]));	//实收本月
			reportVO.setLatefee(setValue(obj[5]));	//滞纳金

			BigDecimal actualPay = new BigDecimal(0);	//实收合计 = 实收往月 + 实收本月 + 滞纳金
			if(!BlankUtil.isBlank(reportVO.getActualBefore())){
				actualPay=actualPay.add(reportVO.getActualBefore());
			}
			if(!BlankUtil.isBlank(reportVO.getActualThis())){
				actualPay=actualPay.add(reportVO.getActualThis());
			}
			if(!BlankUtil.isBlank(reportVO.getLatefee())){
				actualPay=actualPay.add(reportVO.getLatefee());
			}

			reportVO.setActualPay(actualPay); //实收合计
			list.add(reportVO);
		}
		return list;
	}

	//	传入的值必须是 BigDecimal 类型的,否则返回空如果为空或值为0 则返回空
	private BigDecimal setValue(Object obj){
		if (obj instanceof BigDecimal) {
			if(((BigDecimal)obj).intValue() == 0){
				return null;
			}
			return (BigDecimal)obj;
		}
		return null;
	}

	/**
	 * 功能描述:获取头信息  <br>
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-11-13  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private Map<String, Object> getHeadInfo(ManagerPayTenementCollectQueryVO queryVO) {
		Map<String, Object> map = new HashMap<String, Object>();
		String computeMonth = DateUtil.convertDateToStr(queryVO.getComputeMonth(), "yyyy年MM月");
		map.put("reportHeadStr", "计费月份:"+computeMonth);
		map.put("printDate", new Date());

		//根据key获取收费项目名称
		ParameterDefDomain defDomain = parameterDefDao.findUniqueBy("pkey", queryVO.getChargeItemKey());
		String chargeItemName = "";
		if (!BlankUtil.isBlank(defDomain)){
			chargeItemName = defDomain.getPname();
		}
		map.put("chargeItemName", chargeItemName);//收费项目
		return map;
	}

	public void setReportDao(IReportDao reportDao) {
		this.reportDao = reportDao;
	}

	public void setChargeItemDao(IChargeItemDao chargeItemDao) {
		this.chargeItemDao = chargeItemDao;
	}

}
